package com.kit.data.kitopeso.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.JsonObject;
import com.kit.data.kitopeso.KitoApp;
import com.kit.data.kitopeso.PathMaping;
import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.adapter.ApplyAdapter;
import com.kit.data.kitopeso.adapter.CouponsAdapter;
import com.kit.data.kitopeso.analysis.AnalysisUtil;
import com.kit.data.kitopeso.base.BaseActivity;
import com.kit.data.kitopeso.base.BaseBindingAdapter1;
import com.kit.data.kitopeso.base.BaseDialog;
import com.kit.data.kitopeso.base.BaseParams;
import com.kit.data.kitopeso.databinding.KitoApplyActivityBinding;
import com.kit.data.kitopeso.databinding.KitoApplyDaysItemBinding;
import com.kit.data.kitopeso.dialog.TipsDialog;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.net.RxHttp;
import com.kit.data.kitopeso.net.U;
import com.kit.data.kitopeso.obj.KitoApplyDetailInfo;
import com.kit.data.kitopeso.obj.KitoCouponsItem;
import com.kit.data.kitopeso.obj.KitoDetailItemInfo;
import com.kit.data.kitopeso.obj.KitoScoreInfo;
import com.kit.data.kitopeso.obj.KitoUserProcess;
import com.kit.data.kitopeso.tools.AESCryptUtilJar;
import com.kit.data.kitopeso.tools.DateUtils;
import com.kit.data.kitopeso.tools.JsonParse;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.NumUtils;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.SpUtils;
import com.kit.data.kitopeso.tools.ViewUtils;
import com.kit.data.kitopeso.view.KitoNumberPicker;
import com.kit.data.kitopeso.view.KitoSpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class KitoApplyActivity extends BaseActivity<KitoApplyActivityBinding> {


    int amount;

    int minAmount;

    int maxAmout;


    String couponGhs = "0";

    String newCouponGhs = "0";

    String days;

    private final int TIME_WHAT = 1;

    private final int TIME_WHAT1 = 2;


    private int timeCout = 60 * 10;


    List<KitoDetailItemInfo> list1;

    List<KitoDetailItemInfo> list2;

    ApplyAdapter adater1;

    ApplyAdapter adater2;

    int titleCout;

    TipsDialog mTipsDialog;

    KitoApplyDetailInfo applyDetail;

    String currency = "";


    boolean isCanTouch;

    CouponsAdapter couponsAdapter;


    @Override
    protected KitoApplyActivityBinding initViewBinding(LayoutInflater inflater) {
        return KitoApplyActivityBinding.inflate(inflater);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initView() {


        binding.tvBk.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            finish();
        });
        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P33, "");
            startActivity(new Intent(getActivity(), KitoCustomerServiceActivity.class));
        });
        binding.tvBtn.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P36, "");
            if (KitoApp.getInstance().isOrder()) {
                showTipsDialog();
            } else {
                apply();
            }
        });


        binding.tvRefresh.setOnChildScrollUpCallback(new SwipeRefreshLayout.OnChildScrollUpCallback() {
            @Override
            public boolean canChildScrollUp(@NonNull SwipeRefreshLayout parent, @Nullable View child) {
                return binding.tvScrollView.getScrollY() != 0;
            }
        });

        binding.tvRefresh.setOnRefreshListener(() -> {
            PU.userBuriedPoint(PU.P32, "");
            amount = 0;
            getDetailPageForDays(true, false);
            fetchStarWindowConfig();
        });

        binding.tvDate.setOnSelectListener(new KitoNumberPicker.onSelectListener() {
            @Override
            public void onSelect(String text) {
                if (applyDetail != null) {
                    int amount2 = (int) NumUtils.str2Double(text);
                    if (amount2 > amount) {
                        PU.userBuriedPoint(PU.P35, "" + amount2);
                    } else {
                        PU.userBuriedPoint(PU.P34, "" + amount2);
                    }
                    LogUtil.e("me", amount + "<-progress->" + amount2);
                    amount = amount2;
                }
            }
        });

        binding.tvDate.setStartListener(new KitoNumberPicker.onStartListener() {
            @Override
            public void onStart() {
                LogUtil.e("me", "--onStart--");
                isCanTouch = false;
                binding.tvScrollView.setCanTouch(false);
            }

            @Override
            public void onEnd() {
                LogUtil.e("me", "--onEnd--");
                isCanTouch = true;
                binding.tvScrollView.setCanTouch(true);

                binding.tvDate.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getDetailPageForDays(false, true);
                    }
                }, 1000);

            }
        });

//        binding.tvBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                if (binding.tvBar1.isClickable() && applyDetail != null) {
//                    int amount2 = minAmount + progress * applyDetail.getGranularity();
//                    if (amount2 > amount) {
//                        PU.userBuriedPoint(PU.P35, "" + amount2);
//                    } else {
//                        PU.userBuriedPoint(PU.P34, "" + amount2);
//                    }
//                    amount = amount2;
//                    LogUtil.e("me", amount + "<-progress->" + progress + ",max->" + binding.tvBar1.getMax());
//                    binding.tvAmount1.setText(String.valueOf(amount));
//                    binding.tvAmount.setText(String.valueOf(amount));
//                    MyfromUser = fromUser;
//                } else {
//                    binding.tvBar1.setProgress(0);
//                }
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                if (MyfromUser) {
//                    getDetailPageForDays(false, true);
//                    MyfromUser = false;
//                }
//            }
//        });

        list1 = new ArrayList<>();
        list2 = new ArrayList<>();

        LinearLayoutManager manager1 = new LinearLayoutManager(getActivity());
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView1.setLayoutManager(manager1);
        int d = ResUtils.d(R.dimen.dp_6);
        binding.recyclerView1.addItemDecoration(new KitoSpaceItemDecoration(d, d, getActivity()));
        adater1 = new ApplyAdapter(list1);
        binding.recyclerView1.setAdapter(adater1);

        LinearLayoutManager manager2 = new LinearLayoutManager(getActivity());
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView2.setLayoutManager(manager2);
        binding.recyclerView2.addItemDecoration(new KitoSpaceItemDecoration(d, d, getActivity()));
        adater2 = new ApplyAdapter(list2);
        binding.recyclerView2.setAdapter(adater2);


        binding.recyclerView3.setVisibility(View.GONE);
        binding.tvTitle3.setVisibility(View.GONE);

        if (KitoApp.getInstance().getAppInfo() != null && KitoApp.getInstance().getAppInfo().getCouponsInfo() != null && KitoApp.getInstance().getAppInfo().getCouponsInfo().getCouponsItemList() != null) {

            couponsAdapter = new CouponsAdapter(KitoApp.getInstance().getAppInfo().getCouponsInfo().getCouponsItemList());
            couponsAdapter.setOnBtnItemClickListener(new BaseBindingAdapter1.BtnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    couponsAdapter.setIndex(position);
                    couponsAdapter.updateAllData();
                    switchCoupon(KitoApp.getInstance().getAppInfo().getCouponsInfo().getCouponsItemList().get(position));
                }
            });
            LinearLayoutManager manager3 = new LinearLayoutManager(getActivity());
            manager3.setOrientation(LinearLayoutManager.HORIZONTAL);
            binding.recyclerView3.setLayoutManager(manager3);
            binding.recyclerView3.addItemDecoration(new KitoSpaceItemDecoration(d, d, getActivity()));
            binding.recyclerView3.setVerticalFadingEdgeEnabled(false);
            binding.recyclerView3.setVerticalScrollBarEnabled(false);
            binding.recyclerView3.setAdapter(couponsAdapter);

            binding.tvTitle3.setVisibility(View.VISIBLE);

        }


        setTipsText(" 00:00");

        binding.getRoot().setPadding(0, 0, 0, 0);

        setStatusBarHeight(binding.tvRoot);

        binding.tvHit.setVisibility(View.GONE);

    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P29;
        UP_TYPE2 = PU.P30;
        mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
        getDetailPageForDays(false, false);
        fetchStarWindowConfig();

    }


    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == TIME_WHAT) {
                mHandler.removeMessages(TIME_WHAT);
                timeCout = timeCout - 1;
                if (timeCout <= 0) {
                    setTipsText(" 00:00");
                    timeCout = 60 * 10;
                    mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
                } else {
                    String str = DateUtils.convertSecondsToTime(timeCout);
                    setTipsText(" " + str);
                    mHandler.sendEmptyMessageDelayed(TIME_WHAT, 1000);
                }
            } else if (msg.what == TIME_WHAT1) {
//                binding.tvBar1.setProgress(amount);
            }
        }
    };


    void showTipsDialog() {
        if (mTipsDialog == null) {
            mTipsDialog = new TipsDialog(this);
        }
        String date;
        if (KitoApp.getInstance().getAppInfo() != null && !TextUtils.isEmpty(KitoApp.getInstance().getAppInfo().getRejectAfterDays())) {
            date = KitoApp.getInstance().getAppInfo().getRejectAfterDays();
            int day = NumUtils.str2Int(date);
            if (day > 0) {
                date = DateUtils.getNowDateForDays(day);
            }
        } else {
            date = DateUtils.getNowDateForDays(30);
        }
        mTipsDialog.hideCancel(true);
        mTipsDialog.setContentTxt(ResUtils.s(R.string.apply_hit2, date));
        mTipsDialog.show();
    }

    void setTipsText(String date) {
        binding.tvHit.setText(Html.fromHtml(ResUtils.s(R.string.apply_hit, date)));
    }

    @SuppressLint("SetTextI18n")
    void loadData(boolean fromUser) {

        if (getActivity() != null && !isFinishing()) {


            if (applyDetail != null) {

                binding.tvHit.setVisibility(View.VISIBLE);

                list1.clear();
                list2.clear();
                titleCout = 0;

                amount = applyDetail.getAmount();

                minAmount = applyDetail.getMinAmoun();
                maxAmout = applyDetail.getMaxAmount();

                currency = "";
                if (TextUtils.isEmpty(currency)) {
                    currency = SpUtils.get_string(this, SpUtils.KEY_CURRENCY, "");
                }
                if (TextUtils.isEmpty(currency)) {
                    currency = ResUtils.s(R.string.ghs);
                }

                if (couponsAdapter != null) {
                    couponsAdapter.setGhs(currency);
                    couponsAdapter.updateAllData();
                }

                binding.tvDate.setCurrency(currency);
                binding.tvDate.invalidate();


                LogUtil.e("me", maxAmout + "<-maxAmout->" + amount);


                if (maxAmout > minAmount && applyDetail.isChooseAmountFlag()) {


                    if (!fromUser) {
                        List<String> datas = new ArrayList<>();
                        int max = (maxAmout - minAmount) / applyDetail.getGranularity();
                        for (int i = 0; i <= max; i++) {
                            String data = minAmount + applyDetail.getGranularity() * i + "";
                            LogUtil.e("me", amount + "<-data amount->" + data);
                            datas.add(data);
                        }
                        binding.tvDate.setTxtDSize(ResUtils.d(R.dimen.sp_20));
                        binding.tvDate.setViewDistance(ResUtils.d(R.dimen.dp_30));
                        binding.tvDate.setData(datas);
                        binding.tvDate.setSelected(String.valueOf(amount));
                        binding.tvDate.setSelectedColor(Color.parseColor("#7C3AED"));
                        binding.tvDate.setUnSelectedColor(Color.parseColor("#CBD5E1"));


                    }

                    if (amount == minAmount) {
                        binding.tvDate.setSelected(0);
                    }

                } else {
                    if (!fromUser) {

                        List<String> datas = new ArrayList<>();
                        datas.add(String.valueOf(amount));
                        binding.tvDate.setTxtDSize(ResUtils.d(R.dimen.sp_20));
                        binding.tvDate.setViewDistance(ResUtils.d(R.dimen.dp_30));
                        binding.tvDate.setData(datas);
                        binding.tvDate.setSelected(datas.size() - 1);
                        binding.tvDate.setSelectedColor(Color.parseColor("#7C3AED"));
                        binding.tvDate.setUnSelectedColor(Color.parseColor("#CBD5E1"));

                    }

                }

                binding.tvDate.setCanScroll(applyDetail.isChooseAmountFlag());

                if (applyDetail.getDetailItemList() != null) {

                    for (KitoDetailItemInfo itemBean : applyDetail.getDetailItemList()) {
                        if (!TextUtils.isEmpty(itemBean.getShowName()) && !TextUtils.isEmpty(itemBean.getShowDetail())) {

//                            if (itemBean.getShowName().equalsIgnoreCase(ResU.s(R.string.convenience_fee))) {
//                                itemBean.setDiscount("20USD");
//                                itemBean.setOldDetail("100USD");
//                                itemBean.setShowDetail("80USD");
//                            }

                            if (!TextUtils.isEmpty(itemBean.getTitle()) && itemBean.getTitle().equals("YES")) {
                                titleCout = titleCout + 1;
                            }
                            if (titleCout < 2) {
                                list1.add(itemBean);
                            } else {
                                list2.add(itemBean);
                            }

                        }
                    }

//                    KitoDetailItemInfo citem1 = new KitoDetailItemInfo();
//                    citem1.setCoupon(true);
//                    citem1.setShowName(ResUtils.s(R.string.coupons_title2));
//                    citem1.setShowDetail("-" + newCouponGhs + currency);
//                    list1.add(list1.size(), citem1);
//
//                    KitoDetailItemInfo citem2 = new KitoDetailItemInfo();
//                    citem2.setCoupon(true);
//                    citem2.setRePay(true);
//                    citem2.setShowName(ResUtils.s(R.string.coupons_title3));
//                    citem2.setShowDetail("-" + couponGhs + currency);
//                    list2.add(list2.size(), citem2);

                }

                adater1.updateAllData();
                adater2.updateAllData();
                days = applyDetail.getDays();
//                String[] daysArray = new String[]{"100", "300", "500"};
//                applyDetail.setDaysArray(daysArray);

                initTab();

                binding.recyclerView3.setVisibility(View.VISIBLE);


            }


        }

    }

    void switchCoupon(KitoCouponsItem item) {

        if (item != null && !TextUtils.isEmpty(item.getCouponGhs()) && !TextUtils.isEmpty(item.getNewCouponGhs())) {


            for (int i = 0; i < list1.size(); i++) {
                KitoDetailItemInfo item1 = list1.get(i);
                if (item1.isCoupon()) {
                    newCouponGhs = item.getNewCouponGhs();
                    item1.setShowDetail("-" + newCouponGhs + currency);
                }
            }
            for (int i = 0; i < list2.size(); i++) {
                KitoDetailItemInfo item1 = list2.get(i);
                if (item1.isCoupon()) {
                    couponGhs = item.getCouponGhs();
                    item1.setShowDetail("-" + couponGhs + currency);
                }
            }
            adater1.updateAllData();
            adater2.updateAllData();

            amount = amount - NumUtils.str2Int(item.getNewCouponGhs());

            binding.tvDate.postDelayed(new Runnable() {
                @Override
                public void run() {
                    getDetailPageForDays(false, true);
                }
            }, 1000);

        }

    }

    @SuppressLint("SetTextI18n")
    void initTab() {
        if (applyDetail != null) {
            String[] tabs = applyDetail.getDaysArray();
//            String[] tabs = new String[2];
//            tabs[0] = "120";
//            tabs[1] = "200";
            if (tabs != null && tabs.length > 0) {
                days = applyDetail.getDays();
                LogUtil.e("me", "<-days->" + days);
                binding.tvDayLayout.removeAllViews();
                for (int i = 0; i < tabs.length; i++) {
                    String tab = tabs[i];
                    KitoApplyDaysItemBinding binding1 = KitoApplyDaysItemBinding.inflate(LayoutInflater.from(this));
                    binding1.getRoot().setTag(binding1);
                    binding1.tvTitle.setText(tab);
                    binding1.tvTitle.setTag(i);
                    if (tab.equalsIgnoreCase(days)) {
                        binding1.tvTitle.setSelected(true);
                    }
                    if (i == 0) {
                        binding1.tvRoot.setPadding(0, 0, 0, 0);
                    }
                    binding.tvDayLayout.addView(binding1.getRoot());
                    binding1.getRoot().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ViewUtils.viewScale(v);
                            KitoApplyDaysItemBinding binding2 = (KitoApplyDaysItemBinding) v.getTag();
                            days = binding2.tvTitle.getText().toString();
                            switchTab();
                        }
                    });
                }
            } else if (!TextUtils.isEmpty(applyDetail.getDays())) {
                binding.tvDayLayout.removeAllViews();
                String tab = applyDetail.getDays();
                days = tab;
                KitoApplyDaysItemBinding binding1 = KitoApplyDaysItemBinding.inflate(LayoutInflater.from(this));
                binding1.getRoot().setTag(binding1);
                binding1.tvTitle.setText(tab);
                binding1.tvTitle.setSelected(true);
                binding.tvDayLayout.addView(binding1.getRoot());
            }
        }
    }

    void switchTab() {
        int cout = binding.tvDayLayout.getChildCount();
        for (int i = 0; i < cout; i++) {
            View view = binding.tvDayLayout.getChildAt(i);
            KitoApplyDaysItemBinding binding1 = (KitoApplyDaysItemBinding) view.getTag();
            if (binding1.tvTitle.getText().toString().equalsIgnoreCase(days)) {
                binding1.tvTitle.setSelected(true);
            } else {
                binding1.tvTitle.setSelected(false);
            }
        }
        PU.userBuriedPoint(PU.P31, days);
        getDetailPageForDays(false, true);
    }

    void getDetailPageForDays(boolean isRefresh, boolean fromUser) {
        if (binding != null) {
            if (!isRefresh) {
                showLoading("");
            }
            JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[80]);
            json.addProperty("amount", String.valueOf(amount));
            if (!TextUtils.isEmpty(days)) {
                json.addProperty("days", String.valueOf(days));
            }
            json.addProperty("oldFlag", "YES");

            HashMap<String, String> heads = new HashMap<>();
            heads.put("product", U.URL_PRODUCT);
            if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
                heads.put("country", U.URL_COUNTRY);
            }
            String data = AESCryptUtilJar.encryptDefault(json.toString());
            LogUtil.e("me", json + "<-getLoanDetailPageForDays->" + data);
            RxHttp.post(U.commonUrl, data, heads)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<>() {
                        @Override
                        public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                            if (binding != null) {
                                hideLoading();
                                binding.tvRefresh.setRefreshing(false);
                                if (!TextUtils.isEmpty(s)) {
                                    String json1 = AESCryptUtilJar.decryptDefault(s);
                                    LogUtil.e("me", "getLoanDetailPageForDays json->" + json1);
                                    if (JsonParse.getCode(json1)) {
                                        applyDetail = JsonParse.getDetailPageForDaysJson2Obj(json1);
                                        loadData(fromUser);
                                    } else {
                                        showFailedDialog(new BaseDialog.BtnClickListener() {
                                            @Override
                                            public void btnOnClickOk() {
                                                getDetailPageForDays(false, false);
                                                fetchStarWindowConfig();
                                            }

                                            @Override
                                            public void btnOnClickCancel() {

                                            }

                                            @Override
                                            public void btnOnClickColse() {

                                            }
                                        });
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            if (binding != null) {
                                hideLoading();
                                binding.tvRefresh.setRefreshing(false);
                                showFailedDialog(new BaseDialog.BtnClickListener() {
                                    @Override
                                    public void btnOnClickOk() {
                                        getDetailPageForDays(false, false);
                                        fetchStarWindowConfig();
                                    }

                                    @Override
                                    public void btnOnClickCancel() {

                                    }

                                    @Override
                                    public void btnOnClickColse() {

                                    }
                                });
                            }
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    void fetchStarWindowConfig() {
        KitoUserProcess bean = KitoApp.getInstance().getProcess();
        if (!KitoApp.getInstance().isScore() && bean != null && !TextUtils.isEmpty(bean.getUserType()) && bean.getUserType().equals("OLD")) {
            JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[66]);
            HashMap<String, String> heads = new HashMap<>();
            heads.put("product", U.URL_PRODUCT);
            if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
                heads.put("country", U.URL_COUNTRY);
            }
            RxHttp.post(U.commonUrl, AESCryptUtilJar.encryptDefault(json.toString()), heads)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<>() {
                        @Override
                        public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                            if (binding != null) {
                                hideLoading();
                                if (!TextUtils.isEmpty(s)) {
                                    String json1 = AESCryptUtilJar.decryptDefault(s);
                                    LogUtil.e("me", "fetchStarWindowConfig json->" + json1);
                                    if (JsonParse.getCode(json1)) {
                                        KitoScoreInfo scoreBean = JsonParse.fetchStarWindowConfigJson2Obj(json1);
                                        if (scoreBean != null) {
                                            int num = SpUtils.get_int(getActivity(), SpUtils.KEY_SCORE_NUM);
                                            if (!TextUtils.isEmpty(scoreBean.getSwitch_data()) && (scoreBean.getSwitch_data().equals("YES") && num < scoreBean.getNum())) {
                                                KitoApp.getInstance().setScore(true);
                                                num = num + 1;
                                                SpUtils.save_int(getActivity(), SpUtils.KEY_SCORE_NUM, num);
                                                Intent intent = new Intent(getActivity(), KitoScoreActivity.class);
                                                intent.putExtra("content", scoreBean.getContent());
                                                startActivity(intent);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        @Override
                        public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            if (binding != null) {
                                hideLoading();
                            }
                            if (e != null) {
                                LogUtil.e("me", "fetchStarWindowConfig e->" + e);
                            }
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

    void apply() {

        if (amount <= 0) {
            showToastDialog(R.mipmap.hit_icon, ResUtils.s(R.string.apply_hit3));
            return;
        }


        JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[14]);
        json.addProperty("amount", String.valueOf(amount));
        json.addProperty("days", String.valueOf(days));

        HashMap<String, String> heads = new HashMap<>();
        heads.put("product", U.URL_PRODUCT);
        if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
            heads.put("country", U.URL_COUNTRY);
        }
        String data = AESCryptUtilJar.encryptDefault(json.toString());
        LogUtil.e("me", json + "<-apply->" + data);

        showLoading("");

        RxHttp.post(U.commonUrl, data, heads)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull String s) {
                        if (binding != null) {
                            hideLoading();
                            if (!TextUtils.isEmpty(s)) {
                                String json1 = AESCryptUtilJar.decryptDefault(s);
                                LogUtil.e("me", "apply json->" + json1);
                                String orderId = JsonParse.applyJson2Obj(json1);
                                if (JsonParse.getCode(json1)) {
                                    PU.userBuriedAllPoint(getActivity());
                                    AnalysisUtil.INSTANCE.addApplyEvent();
                                    if (!TextUtils.isEmpty(orderId)) {
                                        Intent intent = new Intent(getActivity(), KitoCountdownActivity.class);
                                        intent.putExtra("orderId", orderId);
                                        startActivity(intent);
                                        finish();
                                    }
                                } else {
                                    String msg = JsonParse.getMsg(getActivity(), json1);
                                    LogUtil.e("me", "apply msg->" + msg);
                                    showToastDialog(R.mipmap.hit_icon, msg);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        if (binding != null) {
                            hideLoading();
                            showToastDialog(R.mipmap.hit_icon, getResources().getString(R.string.login_faile));
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeMessages(TIME_WHAT);
            mHandler.removeMessages(TIME_WHAT1);
            mHandler = null;
        }
    }


}
